package com.example.demofdfs.web;

import java.io.IOException;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demofdfs.model.Menu;
import com.example.demofdfs.model.User;
import com.example.demofdfs.service.UserService;
import com.example.demofdfs.web.websocket.WebSocketUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api(value = "用户模块")
public class UserController {
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "新增用户")
	@ApiImplicitParams(value = {@ApiImplicitParam(value = "用户模型", name = "user")}) 
	
	public User add(@RequestBody() @Valid() User user) {
		User u = user;
		
		int result = userService.addUser(u);
		
		return u;
	}
	
	@GetMapping("/get")
	@ApiOperation(value = "获取用户")
	public User get(Integer id) {
		return userService.getUser(id);
	}
	
	@RequestMapping(value = "/get-menu", method = RequestMethod.GET)
	@ApiOperation(value = "新增用户")
	public List<Menu> getMenuList() {
		return null;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	@ApiOperation(value = "修改用户")
	public User update(@RequestBody() User user) {
		
		int i = userService.updateUser(user);
		
		return user;
	}
	
	@GetMapping("/send")
	public Object send(String username, String msg) {
		try {
			WebSocketUtil.send(username, msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msg;
	}
}
